package com.harmontronics.hanma.paas.paas.domain.iam.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.harmontronics.hanma.paas.paas.consts.NumberEnum;
import com.harmontronics.hanma.paas.paas.domain.iam.entity.GroupComponent;
import com.harmontronics.hanma.paas.paas.domain.iam.entity.GroupComponentCreate;
import com.harmontronics.hanma.paas.paas.domain.iam.entity.valueobject.ComponentTypeEnum;
import com.harmontronics.hanma.paas.paas.domain.iam.entity.valueobject.HmUserGroupComponentFileds;
import com.harmontronics.hanma.paas.paas.domain.iam.entity.valueobject.I18nKeyEnum;
import com.harmontronics.hanma.paas.paas.domain.iam.repository.persistence.HmBaseModuleComponentRepository;
import com.harmontronics.hanma.paas.paas.domain.iam.repository.persistence.HmUserGroupComponentRepository;
import com.harmontronics.hanma.paas.paas.domain.iam.entity.HmUserGroupComponent;
import com.harmontronics.hanma.paas.paas.domain.iam.repository.po.HmBaseModuleComponentPO;
import com.harmontronics.hanma.paas.paas.domain.iam.repository.po.HmUserGroupPO;
import com.harmontronics.hanma.paas.paas.exception.ErrorKeyException;
import com.harmontronics.hanma.paas.paas.utils.MyBeanUtils;
import java.util.ArrayList;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.harmontronics.hanma.paas.paas.common.PageBase;

import com.harmontronics.hanma.paas.paas.domain.iam.repository.mapper.HmUserGroupComponentMapper;
import com.harmontronics.hanma.paas.paas.domain.iam.repository.po.HmUserGroupComponentPO;
import java.util.List;

@Service("hmUserGroupComponentService")
@DS("#header.App-Unique")
public class HmUserGroupComponentService {

    @Autowired
    private HmUserGroupComponentRepository hmUserGroupComponentRepository;

    @Autowired
    private HmBaseModuleComponentRepository hmBaseModuleComponentRepository;

    @Autowired
    private HmUserGroupComponentMapper hmUserGroupComponentMapper;

    @Autowired
    private HmUserGroupService hmUserGroupService;


    public List<GroupComponent> getByComponentType(String type, Long groupId) {
        List<GroupComponent> componentDtoList = new ArrayList<>();
        HmUserGroupPO userGroup = hmUserGroupService.selectParentGroupWithChildrenId(groupId);
        // 可见范围从 父节点中选取
        if (ComponentTypeEnum.DATA_VISIBILITY_PERM_COMPONENT.toString().equals(type)) {
            if (userGroup.getParentId() == null) {
                List<HmBaseModuleComponentPO> componentList = hmBaseModuleComponentRepository.list(
                        new QueryWrapper<HmBaseModuleComponentPO>()
                                .select("distinct id,base_module_id,name,code,type,title")
                                .eq(HmUserGroupComponentFileds.TYPE, type));
                componentDtoList = MyBeanUtils.batchTransform(GroupComponent.class, componentList);
            } else {
                componentDtoList = hmUserGroupComponentMapper
                        .selectComponentWithGroupIdAndType(type, userGroup.getId());
            }
        } else {
            // 可管理 及 维保 需要从可见范围中过滤
            /**
            List<HmUserGroupComponentPO> groupComponentList = hmUserGroupComponentRepository.list(
                    new QueryWrapper<HmUserGroupComponentPO>().eq(HmUserGroupComponentFileds.USER_GROUP_ID, groupId)
                            .eq(HmUserGroupComponentFileds.TYPE, NumberEnum.ONE.getNum()));
            if (CollectionUtils.isNotEmpty(groupComponentList)) {
                List<Long> componentIdList = new ArrayList<>();
                for (HmUserGroupComponentPO dto : groupComponentList) {
                    componentIdList.addAll(UserGroupComponentEnum
                            .getCascadeIdListWithId(dto.getComponentId()));
                }
                if (CollectionUtils.isNotEmpty(componentIdList)) {
                    List<HmBaseModuleComponentPO> componentList = hmBaseModuleComponentRepository.list(
                            new QueryWrapper<HmBaseModuleComponentPO>()
                                    .select("distinct id,base_module_id,name,code,type,title")
                                    .eq(HmUserGroupComponentFileds.TYPE, type)
                                    .in(HmUserGroupComponentFileds.ID, componentIdList));
                    componentDtoList = MyBeanUtils.batchTransform(GroupComponent.class, componentList);
                }
            }*/
        }
        return componentDtoList;
    }

    public List<GroupComponent> selectByGroupComponentType(Integer type, Long userGroupId) {
        return hmUserGroupComponentMapper.selectByType(type, userGroupId);
    }

    public void create(GroupComponentCreate groupComponentCreate) {
        List<Long> componentIds = groupComponentCreate.getComponentIds();
        if (CollectionUtils.isNotEmpty(componentIds)) {
            List<ErrorKeyException> exceptionList = new ArrayList<>();
            for (Long componentId : componentIds) {
                try {
                    HmUserGroupComponentPO userGroupComponent =
                            new HmUserGroupComponentPO()
                                    .setUserGroupId(groupComponentCreate.getUserGroupId())
                                    .setComponentId(componentId)
                                    .setType(groupComponentCreate.getType());
                    insertSingleGroupComponent(userGroupComponent);
                } catch (ErrorKeyException e) {
                    exceptionList.add(e);
                }
            }
            if (CollectionUtils.isNotEmpty(exceptionList)) {
                throw exceptionList.get(0);
            }
        }
    }

    private void insertSingleGroupComponent(HmUserGroupComponentPO userGroupComponent) {
        List<HmUserGroupComponentPO> componentList =
                hmUserGroupComponentRepository.list(
                        new QueryWrapper<HmUserGroupComponentPO>()
                                .eq(HmUserGroupComponentFileds.COMPONENT_ID,
                                        userGroupComponent.getComponentId())
                                .eq(HmUserGroupComponentFileds.USER_GROUP_ID,
                                        userGroupComponent.getUserGroupId())
                                .eq(HmUserGroupComponentFileds.TYPE, userGroupComponent.getType()));
        if (CollectionUtils.isNotEmpty(componentList)) {
            throw new ErrorKeyException(I18nKeyEnum.COMPONENT_EXIST);
        }
        hmUserGroupComponentRepository.save(userGroupComponent);
    }

    public void removeById(Long id){
        hmUserGroupComponentRepository.removeById(id);
    }


}